package cn.controller;

import cn.entity.Result;
import cn.entity.Statute;
import cn.service.StatuteService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;

@RestController
@RequestMapping("/statute")
public class StatuteController {
    @Autowired
    StatuteService statuteService;

    @RequestMapping("index")
    public ModelAndView statute(){
        return new ModelAndView("/statute/index");
    }
    @RequestMapping("/select")
    public Result select(@RequestBody HashMap<String,Object> map){

        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        Statute sta = new Statute();
        if (map.get("type")!=null&& (!map.get("type").equals(""))){
            String type = (String) map.get("type");
            sta.setType(Integer.parseInt(type));
        }
        PageHelper.startPage(pageNum,pageSize);
        sta.setDelFlag("0");
        return new Result(true,"成功",new PageInfo<Statute>(statuteService.select(sta),4));
    }

    @RequestMapping("/deleteOne/{id}")
    public int deleteOne(@PathVariable("id")Long id){
        Statute statute = statuteService.selectByPrimaryKey(id);
        statute.setDelFlag("1");
        statute.setUpdateDate(new Date());
        return statuteService.updateByPrimaryKeySelective(statute);
    }

    @RequestMapping("toUpdate")
    public ModelAndView toUpdate(){
        return new ModelAndView("statute/update");
    }

    @RequestMapping("doUpdate")
    public int doUpdate(@RequestBody Statute statute){
        statute.setUpdateDate(new Date());
        return statuteService.updateByPrimaryKeySelective(statute);
    }

    @RequestMapping("/selectByid/{id}")
    public Result selectByid(@PathVariable("id")Long id){
        return new Result(true,"成功",statuteService.selectByPrimaryKey(id));
    }
    @RequestMapping("/insertOne")
    public Result insertOne(@RequestBody Statute statute){
        statute.setUpdateDate(new Date());
        statute.setDelFlag("0");
        return new Result(true,"成功",statuteService.insertSelective(statute));
    }
}
